#include "srcreatewalletpage.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
#include <QLabel>
#include "srregisterpage.h"
#include "subpage/registerpage/srcreatewallet.h"
#include "srregisterpage.h"
#include "srdatabase.h"

SRCreateWalletPage::SRCreateWalletPage(int _mode, QWidget *parent) : OEFramelessWindow(parent)
{
    bIsMoveing_ = true;
    setWindowFlags(Qt::FramelessWindowHint);
    QVBoxLayout* layout = new QVBoxLayout();


    QWidget* wdgt_logo = new QWidget();
    QLabel* lab_logo = new QLabel();
    QString str_logo_title = tr("创建钱包");
    if (_mode != 0) {
        str_logo_title = tr("恢复钱包");
    }
    QLabel* lab_logo_title = new QLabel(str_logo_title);
    lab_logo->setAlignment(Qt::AlignCenter);
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    lab_logo_title->setAlignment(Qt::AlignCenter);
    lab_logo_title->setObjectName(QString("LogoTitle"));
    QHBoxLayout* layout_logo = new QHBoxLayout();
    layout_logo->addSpacing(30);
    layout_logo->addWidget(lab_logo);
    layout_logo->addWidget(lab_logo_title);
    wdgt_logo->setLayout(layout_logo);


    QPushButton* btn_close = new QPushButton("Χ");
    btn_close->setFixedSize(40,40);
    btn_close->setObjectName("CloseButton");
    connect(btn_close, &QPushButton::clicked,
            this, &SRCreateWalletPage::close);

    connect(btn_close, &QPushButton::clicked,
            this, &SRCreateWalletPage::sigClose);

    layout->addWidget(btn_close,0,Qt::AlignRight| Qt::AlignTop);
    layout->addWidget(wdgt_logo,0,Qt::AlignLeft | Qt::AlignTop);
    layout->setMargin(0);



    SRCreateWallet * createWalletPage_ = new SRCreateWallet(_mode);
//    createWalletPage_->setMinimumSize(490,430);
    layout->addWidget(createWalletPage_, 0, Qt::AlignTop | Qt::AlignHCenter);
//    layout->addStretch();

    connect(createWalletPage_, &SRCreateWallet::sigFinshed,
            this, [=](const QString& _walletPath){
//        setMinimumSize(660,600);
        // 删除钱包名称设置窗口
        layout->removeWidget(createWalletPage_);
        createWalletPage_->deleteLater();
        // 打开密语注册窗口
        SRRegisterPage* page = new SRRegisterPage(_mode,this);
        page->setWalletPath(_walletPath);
//        page->setMinimumWidth(600);
        layout->addWidget(page, 0,  Qt::AlignTop| Qt::AlignHCenter);
        layout->addStretch();

        connect(page, &SRRegisterPage::sigRegisterFinshed,
                this, [=]{

            SRDatabase::DB()->writeOperateLog(QString("成功新增钱包 \"%1\"")
                     .arg(_walletPath), 1);
            emit this->sigWalletPath(_walletPath);
            emit this->sigClose();
        });
    });

    QWidget* widget = new QWidget();
    widget->setLayout(layout);
    setCentralWidget(widget);
//    setMinimumSize(660,450);

}

void SRCreateWalletPage::mouseMoveEvent(QMouseEvent *event)
{
    if (!bIsMoveing_)
        return;
    if (movePosition_.isNull()) {
        movePosition_ = event->globalPos() - pos();
    }
    //(event->buttons() && Qt::LeftButton)按下是左键
    //鼠标移动事件需要移动窗口，窗口移动到哪里呢？就是要获取鼠标移动中，窗口在整个屏幕的坐标，然后move到这个坐标，怎么获取坐标？
    //通过事件event->globalPos()知道鼠标坐标，鼠标坐标减去鼠标相对于窗口位置，就是窗口在整个屏幕的坐标
    if (bMoveing_ && (event->buttons() && Qt::LeftButton)
        && (event->globalPos()-movePosition_).manhattanLength() > QApplication::startDragDistance())
    {
        move(event->globalPos()-movePosition_);
        movePosition_ = event->globalPos() - pos();
    }
}

void SRCreateWalletPage::mousePressEvent(QMouseEvent *event)
{

    if (!bIsMoveing_)
        return;
    bMoveing_ = true;
    //记录下鼠标相对于窗口的位置
    //event->globalPos()鼠标按下时，鼠标相对于整个屏幕位置
    //pos() this->pos()鼠标按下时，窗口相对于整个屏幕位置
    movePosition_ = event->globalPos() - pos();

}

void SRCreateWalletPage::mouseReleaseEvent(QMouseEvent *event)
{
    if (!bIsMoveing_)
        return;
    bMoveing_ = false;
}








